package com.itheima.controller;

import com.itheima.entity.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {


    /**
     * 文件上传----本地存储
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        //1.将前端上传的文件存到本地
        String originalFilename = file.getOriginalFilename();
        //2.通过uuid生成随机字符串
        String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        //file.transferTo(new File("/Users/z1216592110icloud.com/Documents/code/javaweb/java147/tlias-web-manager/src/main/resources/jpgs/" + originalFilename));
        file.transferTo(new File("/Users/z1216592110icloud.com/Documents/code/javaweb/java147/tlias-web-manager/src/main/resources/jpgs/" + newFileName));
        return Result.success();
    }

}
